package com.finley.flowabledemo.design.state.demo02;

import com.finley.flowabledemo.design.state.demo02.source.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: xianfei.chao
 * @date 2025/6/24 13:35
 * @version: 1.0
 */
public class SourceCode {

    public static void main(String[] args) {
        Order order = new Order();
        ArrayList<OrderItem> orderItems = new ArrayList<>();
        orderItems.add(new OrderItem("1001", 2));
        orderItems.add(new OrderItem("1002", 3));
        orderItems.add(new OrderItem("1003", 4));
        order.setItems(orderItems);
        order.setId(100001L);
        order.setStatus(OrderStatus.NEW.getCode());
        order.setCustomerEmail("1499022767@qq.com");
        order.setPaymentDetails(orderItems);
        order.setTotalAmount(200.0D);
        // 执行语句
        processOrder(order);
    }

    public static void processOrder(Order order) {
    if (order!= null) {
        if (order.getStatus() == OrderStatus.NEW.getCode()) {
            if (order.getTotalAmount() > 0) {
                if (checkInventory(order.getItems())) {
                    try {
                        DatabaseConnection2 conn = new DatabaseConnection2( "jdbc：mysql：//localhost/shop",  "root",  "password");
                                                            conn.connect();
                                                            conn.executeUpdate( "UPDATE orders SET status='PROCESSING' WHERE id=" + order.getId());
                        EmailSender emailSender = new EmailSender();
                        emailSender.sendOrderConfirmation(order.getCustomerEmail(),  "订单确认",  "您的订单#" + order.getId() +  "已确认");
                        PaymentProcessor paymentProcessor = new PaymentProcessor();
                        boolean paymentResult = paymentProcessor.processPayment(order.getPaymentDetails(), order.getTotalAmount());
                        if (paymentResult) {
                            conn.executeUpdate( "UPDATE orders SET status='PAID' WHERE id=" + order.getId());
                            for (OrderItem item : order.getItems()) {
                                conn.executeUpdate( "UPDATE inventory SET quantity=quantity-" + item.getQuantity() +  " WHERE product_id=" + item.getProductId());
                            }
                            ShippingService shippingService = new ShippingService();
                            shippingService.createShipment(order);
                            conn.executeUpdate( "UPDATE orders SET status='SHIPPED' WHERE id=" + order.getId());
                            emailSender.sendOrderConfirmation(order.getCustomerEmail(),  "订单发货",  "您的订单#" + order.getId() +  "已发货");
                        }
                        conn.close();
                    } catch (Exception e) {
                        System.out.println( "Error： " + e.getMessage());
                    }
                }
            }
        }
    }
    }

    private static  boolean checkInventory(List<OrderItem> items) {
        return true;
    }
}
